﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PSE.Framework.Process;
using PSE.ProcessManager.Entity;

namespace PSE.ProcessManager.Common.Process
{
    public class InteligenceManagerProcessManager : HierarchicalProcessManager<PSE.Framework.Process.HierarchicalProcess,
                                                                               InteligenceManagerInstanceProcess,
                                                                               IProcessService,
                                                                               InteligenceManagerProcessManager>
    {
        protected override Uri GetServiceAddress()
        {
            return new Uri("net.pipe://localhost/InteligenceManager");
        }

        protected override IProcessService CreateServiceInstance()
        {
            return new ProcessService();
        }

        public bool IsChildStarted(string productKey, InstanceEntity instance)
        {
            return this.IsChildStarted(productKey + "_" + instance.Name);
        }

        public InteligenceManagerInstanceProcess StartChild(string productKey, InstanceEntity instance)
        {
            InteligenceManagerInstanceProcess process = this.StartChild(new ChildProcessStartInfo()
                {
                    Name = productKey + "_" + instance.Name,
                    FileName = "InteligenceManagerInstance.exe",
                    CheckParentIsAlive = true,
                    CheckChildIsAlive = true
                });

            process.ProductKey = productKey;
            process.Instance = instance;

            return process;
        }

        public void CloseChild(string productKey, InstanceEntity instance)
        {
            this.CloseChild(productKey + "_" + instance.Name);
        }
    }
}
